package com.jackwoo.jerrymouse.threads;

import com.jackwoo.jerrymouse.bin.SysScanner;
import com.jackwoo.jerrymouse.http.HttpServer;
import com.jackwoo.jerrymouse.http.Request;
import com.jackwoo.jerrymouse.http.Response;
import com.jackwoo.jerrymouse.util.FileUtil;
import com.jackwoo.jerrymouse.util.MagicNumberUtil;
import com.jackwoo.jerrymouse.util.SysStaticResGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.Socket;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Author: StupidZhe
 * Date: 2019/11/29
 * Desc: 产生一个新线程用来处理request
 **/
public class HttpServerThread implements Runnable {



    private Logger logger = LoggerFactory.getLogger(HttpServerThread.class);

    private Socket client;
    private HttpServer httpServer;
    private Request request;


    public HttpServerThread(Socket client, HttpServer httpServer, Request request) {
        this.client = client;
        this.httpServer = httpServer;
        this.request = request;

    }

    @Override
    public void run() {
        try {
            httpServer.process(request, client);
        } catch (FileNotFoundException e) {
            try {
                SysStaticResGenerator.send404(client);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }

    }
}
